package com.etnet.streamserver.view.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.etnet.streamserver.util.StrUtil;
import com.etnet.streamserver.view.service.FieldInfo;
import com.etnet.streamserver.view.service.ViewDataService;

public class ViewFieldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ViewDataService service = new ViewDataService();
    Logger log = Logger.getLogger(ViewFieldServlet.class);

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String result = "0";
        Map<String, FieldInfo> viewFieldMap = this.service.getViewField();
        String fieldid = request.getParameter("fieldid");
        String startObj = request.getParameter("start");
        String limitObj = request.getParameter("limit");
        int start = 0;
        int pageSize = 15;
        if (startObj != null) {
            start = Integer.parseInt(startObj);
        }
        if (limitObj != null) {
            pageSize = Integer.parseInt(limitObj);
        }

        JSONObject obj = new JSONObject();
        try {
            obj.put("resultCode", result);
            if (result.equals("0")) {
                JSONArray array = new JSONArray();
                JSONObject obj2;
                if (fieldid != null) {
                    if (StrUtil.isInteger(fieldid)) {
                        if (viewFieldMap.containsKey(fieldid)) {
                            FieldInfo info = (FieldInfo) viewFieldMap.get(fieldid);
                            obj2 = new JSONObject();
                            obj2.put("id", info.getId());
                            obj2.put("name", info.getName());
                            obj2.put("enable", info.isEnable());
                            obj2.put("reqType", info.getReqType());
                            obj2.put("tip", info.getTip());
                            obj2.put("eng", info.getEng());
                            obj2.put("chi", info.getChi());
                            obj2.put("cht", info.getCht());
                            array.put(obj2);
                        } else {
                            obj.put("resultCode", "-1");
                        }
                    } else if (StrUtil.isCharacter(fieldid)) {
                        for (Map.Entry<String, FieldInfo> entry : viewFieldMap.entrySet()) {
                            FieldInfo info = (FieldInfo) entry.getValue();
                            if (info.getEng().toLowerCase().contains(fieldid.toLowerCase())) {
                                obj2 = new JSONObject();
                                obj2.put("id", info.getId());
                                obj2.put("name", info.getName());
                                obj2.put("enable", info.isEnable());
                                obj2.put("reqType", info.getReqType());
                                obj2.put("tip", info.getTip());
                                obj2.put("eng", info.getEng());
                                obj2.put("chi", info.getChi());
                                obj2.put("cht", info.getCht());
                                array.put(obj2);
                            }
                        }
                    } else {
                        for (Map.Entry<String, FieldInfo> entry : viewFieldMap.entrySet()) {
                            FieldInfo info = (FieldInfo) entry.getValue();
                            if (info.getChi().toLowerCase().contains(fieldid.toLowerCase())) {
                                obj2 = new JSONObject();
                                obj2.put("id", info.getId());
                                obj2.put("name", info.getName());
                                obj2.put("enable", info.isEnable());
                                obj2.put("reqType", info.getReqType());
                                obj2.put("tip", info.getTip());
                                obj2.put("eng", info.getEng());
                                obj2.put("chi", info.getChi());
                                obj2.put("cht", info.getCht());
                                array.put(obj2);
                            }
                        }
                        if (array.length() < 1)
                            for (Map.Entry<String, FieldInfo> entry : viewFieldMap.entrySet()) {
                                FieldInfo info = (FieldInfo) entry.getValue();
                                if (info.getCht().toLowerCase().contains(fieldid.toLowerCase())) {
                                    obj2 = new JSONObject();
                                    obj2.put("id", info.getId());
                                    obj2.put("name", info.getName());
                                    obj2.put("enable", info.isEnable());
                                    obj2.put("reqType", info.getReqType());
                                    obj2.put("tip", info.getTip());
                                    obj2.put("eng", info.getEng());
                                    obj2.put("chi", info.getChi());
                                    obj2.put("cht", info.getCht());
                                    array.put(obj2);
                                }
                            }
                    }
                } else {
                    for (Map.Entry<String, FieldInfo> entry : viewFieldMap.entrySet()) {
                        FieldInfo info = (FieldInfo) entry.getValue();
                        obj2 = new JSONObject();
                        obj2.put("id", info.getId());
                        obj2.put("name", info.getName());
                        obj2.put("enable", info.isEnable());
                        obj2.put("reqType", info.getReqType());
                        obj2.put("tip", info.getTip());
                        obj2.put("eng", info.getEng());
                        obj2.put("chi", info.getChi());
                        obj2.put("cht", info.getCht());
                        array.put(obj2);
                    }
                }

                obj.put("totalCount", array.length());
                obj.put("result", array);
            }
        } catch (JSONException e) {
            this.log.info("create json error!", e);
        }
        out.write(obj.toString());
        out.flush();
        out.close();
    }
}